<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/welcome'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>咨询公告管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--修改对话框-->
    <el-row :gutter="12">
      <el-col :span="6">
        <el-button type="success"
                   icon="el-icon-plus"
                   @click="addDialog">添加</el-button>
      </el-col>
    </el-row>
    <!--表格列表-->
    <el-table :data="tableData">
      <el-table-column prop="consultationAnnouncementId"
                       label="咨询公告id"></el-table-column>
      <el-table-column prop="announcementTitle"
                       label="公告标题"></el-table-column>
      <el-table-column prop="announcementCover"
                       label="公告封面">
                       <template slot-scope="scope">
                        <div class="img-wrapper">
                          <img :src="baseImgUrl+scope.row.announcementCover" alt="">
                        </div>
                       </template>
                       </el-table-column>
      <el-table-column prop="announcementType" :formatter="type"
                       label="公告类别"></el-table-column>
      <el-table-column prop="announcementContent"
                       label="公告内容"></el-table-column>
      <el-table-column align="center"
                       label="操作">
        <template slot-scope="scope">
          <el-button type="success"
                     @click="getOne(scope.row)">修改</el-button>
          <el-button type="danger"
                     @click="deleteUser(scope.row)">删除</el-button>
        </template>

      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="pager.current"
                   :page-size="pager.size"
                   :page-sizes="[5, 10, 15, 20]"
                   :total="pager.total"
                   layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!--修改对话框-->
    <el-dialog :visible.sync="editFlag"
               :close-on-click-modal="false"
               title="修改公告咨询信息">

      <el-form>
        <el-form-item label="咨询公告id">
          <el-input v-model="annos.consultationAnnouncementId"
                    disabled></el-input>
        </el-form-item>
        <el-form-item label="公告标题">
          <el-input v-model="annos.announcementTitle"
                    placeholder="请输入公告标题"></el-input>
        </el-form-item>
        <el-form-item label="公告封面">
          <ImageUpload @getReqUrl="(res,file)=>annos.announcementCover=res"></ImageUpload>
        </el-form-item>
        <el-form-item label="公告类别">
          <el-select v-model="annos.announcementType" placeholder="请输入公告类别">
            <el-option label="公告" value="0"></el-option>
            <el-option label="咨询" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input v-model="annos.announcementContent"
                    placeholder="请输入公告内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="editFlag = false">取 消</el-button>
        <el-button type="primary"
                   @click="update">确 定</el-button>
      </div>
    </el-dialog>
    <!--添加对话框-->
    <el-dialog :visible.sync="addFlag"
               :close-on-click-modal="false"
               title="添加咨询公告信息">
      <el-form>
        <el-form-item label="公告标题">
          <el-input v-model="annos.announcementTitle"
                    placeholder="请输入公告标题"></el-input>
        </el-form-item>
        <el-form-item label="公告封面">
          <ImageUpload @getReqUrl="(res,file)=>annos.announcementCover=res"></ImageUpload>
        </el-form-item>
        <el-form-item label="公告类别">
          <!--<el-input v-model="annos.announcementType"-->
          <!--          placeholder="请输入公告类别"></el-input>-->
          <el-select v-model="annos.announcementType" placeholder="请输入公告类别">
            <el-option label="公告" value="0"></el-option>
            <el-option label="咨询" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input v-model="annos.announcementContent"
                    placeholder="请输入公告内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="addFlag = false">取 消</el-button>
        <el-button type="primary"
                   @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {annoList, deleteAnno, getOneAnno, saveAnno, updateAnno,baseImgUrl} from "../../api/api";
import ImageUpload from "../ImageUpload/index"

export default {
  components:{
    ImageUpload,
  },
  data () {
    return {
      baseImgUrl,
      addFlag: false, // 显示添加对话框的标记
      editFlag: false,
      tableData: [],
      pager: {
        current: 1,
        size: 5,
        total: 0
      },

      listAnno: [],
      annos: {
        consultationAnnouncementId: 0,
        announcementTitle: '',
        announcementCover: '',
        announcementType: '',
        announcementContent: ''
      }

    }
  },
  methods: {
    type(row, colum){
      return row.announcementType == '0' ? '咨询':'公告' ;
    },
    addDialog(){
      this.addFlag = !this.addFlag;
      this.annos.consultationAnnouncementId = 0;
      this.annos.announcementTitle = '';
      this.annos.announcementCover = '';
      this.annos.announcementType = '';
      this.annos.announcementContent = '';
    },
    getOne (row) {
      this.editFlag = !this.editFlag;
      getOneAnno(row.consultationAnnouncementId)
        .then((response) => {
          if (response.code === 200) {
            this.annos = response.data;
          } else {
            this.$message.error("接口出错")
          }
        });
    },
    deleteUser(row){
      deleteAnno(row.consultationAnnouncementId)
        .then((response) => {
          if (response.code === 200) {
            this.$message.success(response.msg);
            this.annoLists(this.pager);
          } else {
            this.$message.error(response.msg);
          }
      });
    },

    update(){
      //前端校验,加钱才做
      //发送请求到后端
      updateAnno(this.annos)
        .then((response) => {
          if (response.code === 200){
            this.$message.success(response.msg);
            this.annoLists(this.pager);
            this.editFlag = false
          }
          else{
            this.$message.error(response.msg);
          }
        });
    },
    save(){
      saveAnno(this.annos)
        .then((response) => {
          if (response.code === 200){
            this.$message.success(response.msg);
            this.annoLists(this.pager);
            this.addFlag = false
          }
          else{
            this.$message.error(response.msg);
          }
        });
    },
    handleCurrentChange (val) {
      console.info("改变分页起始数据" + val);
      this.pager.current = val;
      this.annoLists(this.pager);
    },
    handleSizeChange (val) {
      console.info("改变每页显示的最大条数" + val);
      this.pager.size = val;
      console.log(this.pager)
      this.annoLists(this.pager);
    },
    annoLists (pager) {
      annoList(pager)
        .then((response) => {
          if (response.code === 200) {
            this.$message.success(response.msg);
            this.tableData = response.rows;
            this.pager.total = response.total;
          }
          else {
            this.$message.error(response.msg)
          }
        });
    },
  },
  created () {
    console.log(666)
    this.annoLists(this.pager);
  }
}
</script>

<style>
.el-breadcrumb {
  padding-bottom: 12px;
}
.el-pagination {
  padding-top: 12px;
}
.img-wrapper{
    width: 100px;
    height: 100px;

  }
  .img-wrapper img{
      width: 100px;
      height: 100px;
  }
</style>
